package com.schoolcloud.serve.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.schoolcloud.common.model.PageDTO;
import com.schoolcloud.serve.entity.FoundItems;
import com.schoolcloud.serve.entity.LostFoundCategory;
import com.schoolcloud.serve.entity.LostFoundPic;
import com.schoolcloud.serve.entity.LostItems;
import com.schoolcloud.serve.query.FoundItemQuery;
import com.schoolcloud.serve.service.FoundItemsService;
import com.schoolcloud.serve.mapper.FoundItemsMapper;
import com.schoolcloud.serve.service.LostFoundCategoryService;
import com.schoolcloud.serve.service.LostFoundPicService;
import com.schoolcloud.serve.vo.LostFoundInfoVO;
import com.schoolcolud.daomybatisplus.dto.MpPageUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hongmizfb
 * @description 针对表【found_items(招领表)】的数据库操作Service实现
 * @createDate 2025-03-15 00:09:02
 */
@Service
@RequiredArgsConstructor
public class FoundItemsServiceImpl extends ServiceImpl<FoundItemsMapper, FoundItems>
        implements FoundItemsService {
    private final LostFoundPicService lostFoundPicService;
    private final LostFoundCategoryService lostFoundCategoryService;

    @Override
    public PageDTO<FoundItems> getFoundItemsPage(FoundItemQuery query) {
        String keyword = query.getKeyword();
        Integer categoryId = query.getCategoryId();
        String location = query.getLocation();
        Integer userId = query.getUserId();
        Page<FoundItems> page = lambdaQuery()
                .eq(categoryId != null && categoryId > 0, FoundItems::getCategoryId, categoryId)
                .eq(userId != null, FoundItems::getUserId, userId)
                .like(StrUtil.isNotBlank(location), FoundItems::getLocation, location)
                .like(StrUtil.isNotBlank(keyword), FoundItems::getItemName, keyword)
                .or().like(StrUtil.isNotBlank(keyword), FoundItems::getDescription, keyword)
                .or().like(StrUtil.isNotBlank(keyword), FoundItems::getTitle, keyword)
                .page(query.toMpPage());
        return MpPageUtil.of(page, FoundItems.class);
    }

    @Override
    public LostFoundInfoVO getFoundInfoByFoundId(Integer id) {
        FoundItems byId = getById(id);
        LostFoundInfoVO lostFoundInfoVO = new LostFoundInfoVO();
        BeanUtils.copyProperties(byId, lostFoundInfoVO);
        List<String> collect = lostFoundPicService.lambdaQuery()
                .eq(LostFoundPic::getFoundItemId, id).list()
                .stream().map(LostFoundPic::getUrl).collect(Collectors.toList());
        lostFoundInfoVO.setPicUrls(collect);
        LostFoundCategory category = lostFoundCategoryService.getById(byId.getCategoryId());
        lostFoundInfoVO.setCategoryName(category.getName());
        return lostFoundInfoVO;
    }

    @Override
    public void saveFoundItems(LostFoundInfoVO foundItems) {
        FoundItems foundItems1 = new FoundItems();
        BeanUtils.copyProperties(foundItems, foundItems1);
        if (StrUtil.isBlank( foundItems.getDefaultPicture()) && CollUtil.isNotEmpty(foundItems.getPicUrls())){
            foundItems.setDefaultPicture(foundItems.getPicUrls().get(0));
        }
        if (foundItems.getFoundItemId() == null){
            foundItems.setCreateTime(LocalDateTime.now());
            save(foundItems1);
        }
        if (foundItems.getFoundItemId() != null){
            foundItems1.setUpdateTime(LocalDateTime.now());
            updateById(foundItems1);
            lostFoundPicService.lambdaUpdate().eq(LostFoundPic::getLostItemId, foundItems.getLostItemId()).remove();
        }
        foundItems.getPicUrls().forEach(picUrl -> {
            LostFoundPic lostFoundPic = new LostFoundPic();
            lostFoundPic.setUrl(picUrl);
            lostFoundPic.setFoundItemId(foundItems1.getFoundItemId());
            lostFoundPicService.save(lostFoundPic);
        });
    }
}




